﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmTaiLieu : DevExpress.XtraEditors.XtraForm
    {
        #region Khai baos bien
        TaiLieu_BLL tl_bll = new TaiLieu_BLL();
        DataTable dt;
        TextMessageBox box = new TextMessageBox();
        public string maTaiLieu = "";
        string maNgonNgu = "";
        string maTheLoai = "";
        string maLinhVuc = "";
        string maNguoCung = "";
        string maNhaXB = "";
        string maViTri = "";
        private bool isOk = false;
        DateTime ngayBienMuc;
        #endregion

        #region LoadForm
        public frmTaiLieu()
        {
            InitializeComponent();
        }

        private void frmTaiLieu_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadCombobox();
        }

        #endregion

        #region Cac chuc nang
        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion

        #region cac suwj kieenj
        private void frmTaiLieu_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK; 
            
        }
        private void cboNgonNGu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maNgonNgu = cboNgonNGu.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboTheLoai_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maTheLoai = cboTheLoai.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboLinhVuc_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maLinhVuc = cboLinhVuc.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboNguonCungCap_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maNguoCung = cboNguonCungCap.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboNhaXB_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maNhaXB = cboNhaXB.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboViTri_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maViTri = cboViTri.EditValue.ToString();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region ham viet them
        private void UpdateData()
        {
            if (checkValid())
            {
                TaiLieu_Public tl = new TaiLieu_Public();
                if (maTaiLieu == "")
                {
                    tl.MaTaiLieu = box.AutoID("TL", 2, tl_bll.LayDuLieu());
                    tl.NhanDe =box.toUpper(txtNhanDe.Text);
                    tl.PhuDe = txtPhuDe.Text;
                    tl.TacGiaChinh =box.toUpper(txtTacGiaChinh.Text);
                    tl.DongTacGia = txtDongTG.Text;
                    tl.NguoiBienDich =box.toUpper(txtNguoiBD.Text);
                    tl.TomTatNoiDung = txtTomTat.Text;
                    tl.LanTaiBan = box.ToInt(txtLanTaiBan.Text);
                    tl.NamXuatBan = box.ToInt(txtNamXB.Text);
                    tl.MaNgonNgu = maNgonNgu;
                    tl.MaLinhVuc = maLinhVuc;
                    tl.MaTheLoai = maTheLoai;
                    tl.MaNhaXB = maNhaXB;
                    tl.MaNguonCung = maNguoCung;
                    tl.MaViTri = maViTri;
                    tl.SoTrang = box.ToInt(txtSoTrang.Text);
                    tl.SoLuong = box.ToInt(txtSoLuong.Text);
                    tl.GiaBia = box.ToDouble(txtGiaBia.Text);
                    tl.NgayBienMuc = DateTime.Now;
                    tl.TrangThaiDKCB = false;
                    tl.MaNhanVien = box.MaNhanVien;
                    tl_bll.Insert(tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl,tl,tl);
                    CloseForm();
                }
                else
                {
                    tl.MaTaiLieu = txtMaTaiLieu.Text.Trim();
                    tl.NhanDe =box.toUpper(txtNhanDe.Text);
                    tl.PhuDe = txtPhuDe.Text;
                    tl.TacGiaChinh =box.toUpper(txtTacGiaChinh.Text);
                    tl.DongTacGia = txtDongTG.Text;
                    tl.NguoiBienDich =box.toUpper(txtNguoiBD.Text);
                    tl.TomTatNoiDung = txtTomTat.Text;
                    tl.LanTaiBan = box.ToInt(txtLanTaiBan.Text);
                    tl.NamXuatBan = box.ToInt(txtNamXB.Text);
                    tl.MaNgonNgu = maNgonNgu;
                    tl.MaLinhVuc = maLinhVuc;
                    tl.MaTheLoai = maTheLoai;
                    tl.MaNhaXB = maNhaXB;
                    tl.MaNguonCung = maNguoCung;
                    tl.MaViTri = maViTri;
                    tl.SoTrang = box.ToInt(txtSoTrang.Text);
                    tl.SoLuong = box.ToInt(txtSoLuong.Text);
                    tl.GiaBia = box.ToDouble(txtGiaBia.Text);
                    tl.NgayBienMuc = ngayBienMuc;
                    tl.TrangThaiDKCB = false;
                    tl.MaNhanVien = box.MaNhanVien;
                    tl_bll.Update(tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl,tl,tl);
                    CloseForm();
                }
            }
        }
        private void CloseForm()
        {
            if (isOk == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }
        private void LoadData()
        {
            if (maTaiLieu != "")
            {
                dt = tl_bll.TaoBang(box.sqlCheckExist("TaiLieu","MaTaiLieu",maTaiLieu));
                DataRow dr=dt.Rows[0];
                txtMaTaiLieu.Text=dr["MaTaiLieu"].ToString();
                txtNhanDe.Text=dr["NhanDe"].ToString();
                txtPhuDe.Text=dr["PhuDe"].ToString();
                txtTacGiaChinh.Text = dr["TacGiaChinh"].ToString();
                txtDongTG.Text=dr["DongTacGia"].ToString();
                txtNguoiBD.Text = dr["NguoiBienDich"].ToString();
                txtTomTat.Text = dr["TomTatNoiDung"].ToString();
                txtLanTaiBan.Text = dr["LanTaiBan"].ToString();
                txtNamXB.Text = dr["NamXuatBan"].ToString();
                maNgonNgu=dr["MaNgonNgu"].ToString();
                maLinhVuc = dr["MaLinhVuc"].ToString();
                maTheLoai = dr["MaTheLoai"].ToString();
                maNhaXB = dr["MaNhaXB"].ToString();
                maNguoCung=dr["MaNguonCung"].ToString();
                maViTri = dr["MaViTri"].ToString();
                txtSoTrang.Text = dr["SoTrang"].ToString();
                txtSoLuong.Text=dr["SoLuong"].ToString();
                txtGiaBia.Text = dr["GiaBia"].ToString();
                ngayBienMuc = Convert.ToDateTime(dr["NgayBienMuc"]);
            }
        }
        private bool checkValid()
        {

            if (txtNhanDe.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Nhan đề tài liệu.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtNhanDe.Focus();
                return false;
            }
            if (txtTacGiaChinh.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tác giả chinh.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTacGiaChinh.Focus();
                return false;
            }
            if (cboNgonNGu.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Ngôn Ngữ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboNgonNGu.Focus();
                return false;
            }
            if (cboLinhVuc.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Lĩnh vực.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboLinhVuc.Focus();
                return false;
            }
            if (cboTheLoai.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Thể loại.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboTheLoai.Focus();
                return false;
            }
            if (cboNhaXB.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Nhà xuất bản.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboNhaXB.Focus();
                return false;
            }
            if (cboNguonCungCap.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Nguồn cung cấp.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboNguonCungCap.Focus();
                return false;
            }
            if (cboViTri.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Vị trí lưu trữ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboViTri.Focus();
                return false;
            }
            if (txtSoLuong.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Số lượng.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtSoLuong.Focus();
                return false;
            }
            if (txtGiaBia.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Giá bìa.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtGiaBia.Focus();
                return false;
            }
            return true;
        }
        private bool checkMaInsert()
        {
            dt = tl_bll.TaoBang(box.sqlCheckExist("TaiLieu","MaTaiLieu",txtMaTaiLieu.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtMaTaiLieu.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtMaTaiLieu.Focus();
                return false;

            }
            return true;
        }
        private bool checkTenInsert()
        {
            string sql = "select NhanDe,TacGiaChinh from TaiLieu where TenTaiLieu=N'"+txtNhanDe.Text.Trim()+"' and TacGiaChinh=N'"+txtTacGiaChinh.Text.Trim()+"'";
            dt = tl_bll.TaoBang(sql);
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show("Tài liệu này đã tồn tại.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtNhanDe.Focus();
                return false;


            }
            return true;
        }
        private void SetInterface()
        {
            box.Huy_Text(this);
            cboLinhVuc.Text = "";
            cboNgonNGu.Text = "";
            cboNguonCungCap.Text = "";
            cboNhaXB.Text = "";
            cboTheLoai.Text = "";
            cboViTri.Text = "";
            maTaiLieu = "";
            txtNhanDe.Focus();
            txtMaTaiLieu.Text = "Mã tự sinh";
        }
        private void LoadCombobox()
        {
            //linh vuc
            cboLinhVuc.Properties.DataSource = tl_bll.TaoBang(box.sqlSelectFile("MaLinhVuc","TenLinhVuc","DM_LinhVuc"));
            cboLinhVuc.Properties.DisplayMember = "TenLinhVuc";
            cboLinhVuc.Properties.ValueMember = "MaLinhVuc";
            //
            //ngon ngu
            cboNgonNGu.Properties.DataSource = tl_bll.TaoBang(box.sqlSelectFile("MaNgonNgu","TenNgonNgu","DM_NgonNgu"));
            cboNgonNGu.Properties.DisplayMember = "TenNgonNgu";
            cboNgonNGu.Properties.ValueMember = "MaNgonNgu";
            //nguon cung cap
            cboNguonCungCap.Properties.DataSource = tl_bll.TaoBang(box.sqlSelectFile("MaNguonCung", "TenNguonCung", "DM_NguonCungCap"));
            cboNguonCungCap.Properties.DisplayMember ="TenNguonCung";
            cboNguonCungCap.Properties.ValueMember = "MaNguonCung";
            //NhaXB
            cboNhaXB.Properties.DataSource = tl_bll.TaoBang(box.sqlSelectFile("MaNhaXB","TenNhaXB","DM_NhaXuatBan"));
            cboNhaXB.Properties.DisplayMember = "TenNhaXB";
            cboNhaXB.Properties.ValueMember = "MaNhaXB";
            //the loai
            cboTheLoai.Properties.DataSource=tl_bll.TaoBang(box.sqlSelectFile("MaTheLoai","TenTheLoai","DM_TheLoaiTaiLieu"));
            cboTheLoai.Properties.DisplayMember = "TenTheLoai";
            cboTheLoai.Properties.ValueMember = "MaTheLoai";
            //vitri
            cboViTri.Properties.DataSource = tl_bll.TaoBang(box.sqlSelectFile("MaViTri","TenViTri","DM_ViTriLuuTru"));
            cboViTri.Properties.DisplayMember = "TenViTri";
            cboViTri.Properties.ValueMember = "MaViTri";
        }
        #endregion


    }
}